package com.cenwangkeji.resident.presenter.fragment;

import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.BannerBean;
import com.cenwangkeji.resident.model.bean.BasicInfoBean;
import com.cenwangkeji.resident.model.bean.CommunityNoticeBean;
import com.cenwangkeji.resident.model.bean.HealthContentBean;
import com.cenwangkeji.resident.model.bean.HealthTypeBean;
import com.cenwangkeji.resident.model.bean.User;
import com.google.gson.Gson;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.GsonUtils;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * @松 on 2019/10/29 12:36
 */
public class MyPresenter {
    private I mI;

    public MyPresenter(I i) {
        mI = i;
    }

    public void getUserDetail(){
        Observable.create(new ObservableOnSubscribe<BasicInfoBean>() {
            @Override
            public void subscribe(ObservableEmitter<BasicInfoBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put( Constant.token, User.getToken());
                map.put( "userId",User.getUserId());
                map.put( "mark",3);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.user_detail);
                Gson gson = GsonUtils.getInstance();
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json,BasicInfoBean.class));
            }
        }).compose(RxUtils.<BasicInfoBean>io_main()).subscribe(new ObserverOnNext<BasicInfoBean>() {
            @Override
            public void onNext(BasicInfoBean bean) {
                if (mI !=null ){
                    mI.onUserDetail(bean);
                }
            }
        });

    }

    public interface I{
        void onUserDetail(BasicInfoBean bean);

    }
}
